$(function() {
	$.ajax({
		url:"../../../portal/open/hivelearn/findNewTopic.json",
		success:function(data){
			if(data.data){
				var text = data.data.deptName+"部门，"+data.data.userName+"创建了："+data.data.topicTitle;
				$(".topic-info").attr("title",text);
				if(text&&text.length>40){
					text = text.substring(0, 40)+"...";
				}
				$(".topic-info").text(text);
			}
		}
	});
	$(".sanjiao").click(function() {
		$(".sanjiao").toggleClass("glyphicon-triangle-top").toggleClass("glyphicon-triangle-bottom");
		$(".user-search").slideToggle("slow");
	});
	$(".searchicon").click(function(){
		$(".user-search").slideDown("slow");
		$(".sanjiao").removeClass("glyphicon-triangle-top").addClass("glyphicon-triangle-bottom");
	});
	var search = location.search;
	if(search.indexOf("error")!=-1){
		$("#userCheck").show();
	}else{
		$("#userCheck").hide();
	}
	$("#PverificationImg").click(function(){
		$("#PverificationImg").attr("src","../../../portal/captcha/generateCaptcha.do?num="+Math.random());
	});
	$.ajax({
		url:"../../../portal/open/getCurrentUser.json",
		success:function(data){
			if(data.data.isLogin){
				$(".login-info").show();
				$(".user-login").hide();
				$(".user-name").text(data.data.user.loginID);
				$(".user-name").attr("title",data.data.user.loginID);
				var photo = data.data.user.extendAccountInfo.headPhoto;
				$.ajax({
					url:"../../../hivelearn/hivelearnactivity/findCountScore.json?userId="+data.data.user.entityID,
					success:function(data1){
						$(".user-score").text("已获得学时：" + data1.data + "学时");
					}
				});
				if(photo){
					$(".user-img").find("img").attr("src","../../../components/gtattachment/open/downloadAttachment.json?attachmentId="+photo);
				}
			}else{
				$(".login-info").hide();
				$(".user-login").show();
			}
		}
	});
	
	//点击搜索事件
	$("#courseSearch").click(function(){
	   var val = $(this).closest(".input-group").find("input").val();
	   if(/^\s*$/.test(val)){
	   $(this).closest(".input-group").find("input");
	   return;
	   }
	   window.location.href="../../../portal/frontend/dgo?ftlpath=course1.ftl#"+val;  
	    
	});
	//监听搜索的键盘事件
		$(function(){
			$("#search").bind('focus',function(){
				 var self = $(this);
				 var event = event||window.event;
				$("#search").keydown(function (event) {
					var val = self.val();
					 if(event.keyCode == 13){
					  if(/^\s*$/.test(val)){
						  self.attr('placeholder','内容不能为空');
						  return;
					  }
						window.location.href = '../../../portal/frontend/dgo?ftlpath=course1.ftl#'+val; 
					 }
				})
				
			})
		})
		//失去焦点让默认文本恢复
	   $(function(){
		   $('.navbar-nav .input-group2 input').bind('blur',function(){
			   var val = $(this).val();
			   if(/^\s*$/.test(val)){
			   $(this).val('').attr('placeholder','请搜索课程');
			   }
			   
		   })
	   })
});

//登录验证
function loginValidation(){
 	var code= $('input[name="PverificationCode"]')[0].value;
 	var validFlag = false;
 	if(!code){
 		$("#verificationCode").show();
 		$("#userCheck").hide();
 		$("#verificationCode").text("验证码不能为空");
 		return false;
 	}
    $.ajax({
      url: '../../../portal/captcha/verifyCaptcha.json?captcha='+code,
      type: 'get',
      async: false,    
      dataType: 'json',
      success: function(result){
      if(!result.data.isSuccess){
      	$("#verificationCode").show();
      	$("#userCheck").hide();
 		$("#verificationCode").text("验证码错误");
        validFlag=false;
        $("#PverificationImg").attr("src","../../../portal/captcha/generateCaptcha.do?num="+Math.random());
      }else{
      	$("#userCheck").hide();
      	$("#verificationCode").text("");
        validFlag=true;
        }
      }
    });
    if(!validFlag){
		return false;
    }
    $("#verificationCode").text("");
    $("#userCheck").hide();
	var pwd = $("#login_password").val();
	$("#login_password").val($.md5(pwd));
}